package cn.itcast.web;

import cn.itcast.domain.Student;
import cn.itcast.service.Impl.studentServiceImpl;
import cn.itcast.service.studentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @param
 * @return
 */
@WebServlet("/studentServlet/*")
public class studentServlet extends BeanServlet {
    //在成员位置多态创建service业务层对象复用，下面的每个方法都要用到
    studentService service = new studentServiceImpl();

    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //先防止乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String ck = request.getParameter("ck");
        //调用业务层方法，接受查询结果
        boolean login = service.login(username, password);
        if(login){//查到了
            //这里还要完成一个记住操作的操作，只有登录成功的时候才会记住，使用cookie
            Cookie usernameCookie = new Cookie("username",username);
            Cookie passwordCookie = new Cookie("password",password);
            //将登录信息值存进session
            HttpSession session = request.getSession();
            session.setAttribute("username",username);
            session.setAttribute("password",password);
            if("1".equals(ck)){
                //设置有效期
                usernameCookie.setMaxAge(3600);
                passwordCookie.setMaxAge(3600);
            }else {
                //设置有效期
                usernameCookie.setMaxAge(0);
                passwordCookie.setMaxAge(0);
            }
            //设置路径
            usernameCookie.setPath("/");
            passwordCookie.setPath("/");
            //将Cookie添加到响应对象上
            response.addCookie(usernameCookie);
            response.addCookie(passwordCookie);

            //转发到首页
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }else {
            //查询不到，登录失败，转发到登录页面
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //添加学生方法,获取参数
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String score = request.getParameter("score");
        //创建学生对象，封装上面获取的表单信息
        Student student = new Student(name, Integer.parseInt(age), Integer.parseInt(score));
        //调用业务层方法，将封装了数据的学生对象传参,方法没有返回值
        service.add(student);
        //添加完成后重定向到首页，避免路径不变，刷新时重复提交
        response.sendRedirect(request.getContextPath()+"/index.jsp");
       // request.getRequestDispatcher("/index.jsp").forward(request,response);
    }
    public void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取输入流将文件中的数据读取到内存中
        BufferedReader br = new BufferedReader(new FileReader("d://stu.txt"));
      //定义Student类型的集合，存储文件中的多个学生对象
        ArrayList<Student> list = new ArrayList<>();
        //定义空字符串
        String len;
        //循环读取信息，一次读取一行
        while ((len=br.readLine())!=null){
            //定义学生类
            Student student = new Student();
            //切割字符串
            String[] split = len.split(",");
            student.setName(split[0]);
            student.setAge(Integer.parseInt(split[1]));
            student.setScore(Integer.parseInt(split[2]));
            list.add(student);
        }
        //遍历集合，将获取的学生对象的数据写出到页面，乱码问题在当前类的父类中以及解决了
        for (Student student : list) {
            response.getWriter().write(student.getName()+","+student.getAge()+","+student.getScore());
            response.getWriter().write("<br>");
        }
    }
}
